package com.fashiontextile.admin.controller;

import com.fashiontextile.admin.entity.Dict;
import com.fashiontextile.admin.entity.DictCategory;
import com.fashiontextile.admin.service.DictService;
import com.fashiontextile.common.annotation.RequireRole;
import com.fashiontextile.common.constant.AppConstants;
import com.fashiontextile.common.entity.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/dict")
public class DictController {
    DictService dictService;

    @GetMapping("/id/{did}")
    public R<Dict> selectDict(@PathVariable("did") Long did) {
        Dict dict = dictService.selectDictByDid(did);
        if (dict != null) {
            return R.success(dict);
        } else {
            return R.fail("字典不存在");
        }
    }

    @GetMapping("/category/{dcid}")
    public R<?> selectDictByCategory(@PathVariable Long dcid,
                                     @RequestParam(required = false, defaultValue = "1") Integer page) {
        PageHelper.startPage(page, 10);
        List<Dict> dicts = dictService.selectDictByCategoryId(dcid);
        if (dicts == null) {
            return R.fail("字典类型不存在或该分类下无字典");
        }
        PageInfo<Dict> dictPageInfo = new PageInfo<>(dicts);
        if (dicts.size() > 0) {
            return R.success(dictPageInfo);
        } else {
            return R.fail("字典类型不存在或该分类下无字典");
        }
    }

    @GetMapping("/category")
    public R<?> getAllDictcategory() {
        List<DictCategory> categories = dictService.selectAllDictCategory();
        if (categories != null && categories.size() > 0) {
            return R.success(categories);
        } else {
            return R.fail("字典类型不存在");
        }
    }

    @RequireRole(AppConstants.ROLE_ADMIN)
    @PostMapping("")
    public R<?> addDict(@RequestBody Dict dict) {
        DictCategory dictCategory = dictService.selectDictCategoryBydicd(dict.getCategory().getDcid());
        if (dictCategory == null) {
            return R.fail("字典类型不存在");
        }
        Dict resultDict = dictService.selectDictbyTypeAndValueAndParent(dict.getCategory().getDcid(), dict.getValue(), dict.getParentId());
        if (resultDict != null) {
            return R.fail("字典值已存在");
        }
        Integer insertResult = dictService.insertDict(dict);
        return insertResult > 0 ? R.success("字典添加成功") : R.fail("字典添加失败");
    }

    @RequireRole(AppConstants.ROLE_ADMIN)
    @DeleteMapping("/id/{did}")
    public R<?> deleteDictByDid(@PathVariable("did") Long did) {
        Dict dict = dictService.selectDictByDid(did);
        if (dict == null) {
            return R.fail("字典不存在");
        }
        Integer deleteResult = dictService.deleteDictByDid(did);
        return deleteResult > 0 ? R.success("字典删除成功") : R.fail("字典删除失败");
    }

    @RequireRole(AppConstants.ROLE_ADMIN)
    @PostMapping("/category")
    public R<?> addDictCategory(@RequestBody DictCategory newdictCategory) {
        DictCategory dictCategory = dictService.selectDictcategoryByname(newdictCategory.getName());
        if (dictCategory != null) {
            return R.fail("字典类型已经存在");
        }
        Integer insertResult = dictService.insertDictcategory(newdictCategory.getName());
        return insertResult > 0 ? R.success("字典类型添加成功") : R.fail("字典类型添加失败");
    }

    @DeleteMapping("/category/id/{dcid}")
    public R<?> deleteCategoryByDcid(@PathVariable("dcid") Long dcid) {
        DictCategory dictCategory = dictService.selectDictCategoryBydicd(dcid);
        if (dictCategory == null) {
            return R.fail("字典类型不存在");
        }
        Integer deleteResult = dictService.deleteDictCategoryByDcid(dcid);
        return deleteResult > 0 ? R.success("字典类型删除成功") : R.fail("字典类型删除失败");
    }
}
